package tmhm.shemes;

import java.awt.Point;
import java.util.ArrayList;

import tmhm.components.*;
import tmhm.gui.*;


public class Adr1 {

    private static IntToInt IBUS1ToGPRAR;
    private static REG GPRAR;
    private static GPR GPRegs;
    private static TSB TSBGPRout1;
    private static REG SP;
    private static TSB TSBSPout2;
    private static ADD ADDadr;
    private static REG CW;
    private static TSB TSBADDout2;
    private static TSB TSBCWout3;
    private static GuiScheme gui;

    public static GuiScheme getGui() {
        return gui;
    }

    public static void init() {
        IBUS1ToGPRAR = new IntToInt(16, 5,false,false);

        GPRAR = new REG(1, "GPRAR");
        GPRAR.getOut(0).setIsInt();
        GPRAR.getOut(0).setNumOfLines(5);

        GPRegs = new GPR(32);
        GPRegs.getOut(0).setNumOfLines(16);

        TSBGPRout1 = new TSB();
        TSBGPRout1.getOut(0).setNumOfLines(16);

        SP = new REG(1, "SP");
        SP.getOut(0).setIsInt();
        SP.getOut(0).setNumOfLines(16);
        SP.initVal(4096);//1000h

        TSBSPout2 = new TSB();
        TSBSPout2.getOut(0).setNumOfLines(16);

        ADDadr = new ADD();
        ADDadr.getOut(0).setIsInt();
        ADDadr.getOut(0).setNumOfLines(16);

        CW = new REG(1, "PR");
        CW.getOut(0).setIsInt();
        CW.getOut(0).setNumOfLines(16);

        TSBCWout3 = new TSB();
        TSBCWout3.getOut(0).setNumOfLines(16);
        
        TSBADDout2 = new TSB();
        TSBADDout2.getOut(0).setNumOfLines(16);

    }

    public static void connect() {
        IBUS1ToGPRAR.setInputPin(0, Bus1.IBUS1());

        GPRAR.setInputPin(0, IBUS1ToGPRAR.getOut(0));
        GPRAR.setPinLd(Oper1.ldGPRAR());
        GPRAR.setPinInc(Oper1.incGPRAR());

        GPRegs.setAdressPin(GPRAR.getOut(0));
        GPRegs.setInputDataPin(Bus1.IBUS3());
        GPRegs.setWrite(Oper1.wrGPR());
        GPRegs.setRead(new Pin(true));

        TSBGPRout1.setInputPin(0, GPRegs.getOut(0));
        TSBGPRout1.setCtrl(Oper1.GPRout1());
        Bus1.addOnIBUS1(TSBGPRout1.getOut(0));

        SP.setInputPin(0, Bus1.IBUS3());
        SP.setPinInc(Oper1.incSP());
        SP.setPinDec(Oper1.decSP());
        SP.setPinLd(Oper1.ldSP());

        TSBSPout2.setInputPin(0, SP.getOut(0));
        TSBSPout2.setCtrl(Oper1.SPout2());
        Bus1.addOnIBUS2(TSBSPout2.getOut(0));

        CW.setInputPin(0, Bus1.IBUS2());
        CW.setPinLd(Oper1.ldCW());

        TSBCWout3.setInputPin(0, CW.getOut(0));
        TSBCWout3.setCtrl(Oper1.CWout3());
        Bus1.addOnIBUS3(TSBCWout3.getOut(0));
        
        ADDadr.setPinA(Bus1.IBUS1());
        ADDadr.setPinB(Bus1.IBUS3());
        
        
        TSBADDout2.setInputPin(0, ADDadr.getOut(0));
        TSBADDout2.setCtrl(Oper1.ADDout2());
        Bus1.addOnIBUS2(TSBADDout2.getOut(0)); 

    }

    public static void gui() {
        GuiLine line; // Pomocna promenljiva
        gui = new GuiScheme("tmhm/tmhm/images/Adr1.png");

        ArrayList<ArrayList<Point>> sections;
        ArrayList<Point> points;




        sections=new ArrayList<ArrayList<Point>>();
        points=new ArrayList<Point>();
        points.add(new Point(589,7));
        points.add(new Point(589,736));
        sections.add(points);
        line=new GuiLine(sections,Bus1.IBUS1());
        gui.addLine(line);
        sections=new ArrayList<ArrayList<Point>>();
        points=new ArrayList<Point>();
        points.add(new Point(570,22));
        points.add(new Point(570,721));
        sections.add(points);
        line=new GuiLine(sections,Bus1.IBUS2());
        gui.addLine(line);
        sections=new ArrayList<ArrayList<Point>>();
        points=new ArrayList<Point>();
        points.add(new Point(551,37));
        points.add(new Point(551,705));
        sections.add(points);
        line=new GuiLine(sections,Bus1.IBUS3());
        gui.addLine(line);
        
        

        sections=new ArrayList<ArrayList<Point>>();
        points=new ArrayList<Point>();
        points.add(new Point(242,93));
        points.add(new Point(242,74));
        points.add(new Point(588,74));
        sections.add(points);
        line=new GuiLine(sections,IBUS1ToGPRAR.getOut(0));
        gui.addLine(line);

        

        sections=new ArrayList<ArrayList<Point>>();
        points=new ArrayList<Point>();
        points.add(new Point(160,103));
        points.add(new Point(167,103));
        sections.add(points);
        line=new GuiLine(sections,Oper1.incGPRAR());
        gui.addLine(line);
        sections=new ArrayList<ArrayList<Point>>();
        points=new ArrayList<Point>();
        points.add(new Point(160,121));
        points.add(new Point(167,121));
        sections.add(points);
        line=new GuiLine(sections,Oper1.ldGPRAR());
        gui.addLine(line);


        

sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(317,112));
points.add(new Point(324,112));
sections.add(points);
line=new GuiLine(sections,LogicalComponent.getCPUCLK());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(242,131));
points.add(new Point(242,213));
points.add(new Point(268,213));
sections.add(points);
line=new GuiLine(sections,GPRAR.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(419,213));
points.add(new Point(426,213));
sections.add(points);
line=new GuiLine(sections,Oper1.wrGPR());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(344,251));
points.add(new Point(344,279));
points.add(new Point(412,279));
sections.add(points);
line=new GuiLine(sections,GPRegs.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(423,284));
points.add(new Point(423,290));
sections.add(points);
line=new GuiLine(sections,Oper1.GPRout1());
gui.addLine(line);


sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(435,279));
points.add(new Point(589,279));
sections.add(points);
line=new GuiLine(sections,TSBGPRout1.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(325,334));
points.add(new Point(325,315));
points.add(new Point(551,315));
sections.add(points);
line=new GuiLine(sections,Bus1.IBUS3());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(242,343));
points.add(new Point(250,343));
sections.add(points);
line=new GuiLine(sections,Oper1.incSP());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(242,362));
points.add(new Point(250,362));
sections.add(points);
line=new GuiLine(sections,Oper1.decSP());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(400,343));
points.add(new Point(407,343));
sections.add(points);
line=new GuiLine(sections,Oper1.ldSP());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(400,362));
points.add(new Point(407,362));
sections.add(points);
line=new GuiLine(sections,LogicalComponent.getCPUCLK());
gui.addLine(line);


sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(325,371));
points.add(new Point(325,400));
points.add(new Point(412,400));
sections.add(points);
line=new GuiLine(sections,SP.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(423,404));
points.add(new Point(423,411));
sections.add(points);
line=new GuiLine(sections,Oper1.SPout2());
gui.addLine(line);

sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(434,400));
points.add(new Point(570,400));
sections.add(points);
line=new GuiLine(sections,TSBSPout2.getOut(0));
gui.addLine(line);



sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(325,655));
points.add(new Point(325,676));
points.add(new Point(570,676));
sections.add(points);
line=new GuiLine(sections,Bus1.IBUS2());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(242,646));
points.add(new Point(250,646));
sections.add(points);
line=new GuiLine(sections,Oper1.ldCW());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(242,627));
points.add(new Point(250,627));
sections.add(points);
line=new GuiLine(sections,LogicalComponent.getCPUCLK());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(325,618));
points.add(new Point(325,580));
points.add(new Point(412,580));
sections.add(points);
line=new GuiLine(sections,CW.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(325,486));
points.add(new Point(325,447));
points.add(new Point(412,447));
sections.add(points);
line=new GuiLine(sections,ADDadr.getOut(0));
gui.addLine(line);


sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(423,452));
points.add(new Point(423,458));
sections.add(points);
line=new GuiLine(sections,Oper1.ADDout2());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(433,447));
points.add(new Point(570,447));
sections.add(points);
line=new GuiLine(sections,TSBADDout2.getOut(0));
gui.addLine(line);


sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(433,580));
points.add(new Point(551,580));
sections.add(points);
line=new GuiLine(sections,TSBCWout3.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(423,585));
points.add(new Point(423,591));
sections.add(points);
line=new GuiLine(sections,Oper1.ldCW());
gui.addLine(line);


sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(287,522));
points.add(new Point(287,553));
points.add(new Point(551,553));
sections.add(points);
line=new GuiLine(sections,Bus1.IBUS3());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(363,522));
points.add(new Point(363,541));
points.add(new Point(589,541));
sections.add(points);
line=new GuiLine(sections,Bus1.IBUS1());
gui.addLine(line);


sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(344,176));
points.add(new Point(344,150));
points.add(new Point(551,150));
sections.add(points);
line=new GuiLine(sections,Bus1.IBUS3());
gui.addLine(line);


        // LABELE:
        gui.addLabel(new GuiLabel(593, 37, Bus1.IBUS1()));
        gui.addLabel(new GuiLabel(384, 536, Bus1.IBUS1()));
        gui.addLabel(new GuiLabel(572, 54, Bus1.IBUS2()));
        gui.addLabel(new GuiLabel(500, 671, Bus1.IBUS2()));
        gui.addLabel(new GuiLabel(521, 115, Bus1.IBUS3()));
        gui.addLabel(new GuiLabel(441, 146, Bus1.IBUS3()));
        gui.addLabel(new GuiLabel(494, 311, Bus1.IBUS3()));
        gui.addLabel(new GuiLabel(293, 548, Bus1.IBUS3()));
        gui.addLabel(new GuiLabel(338, 71, IBUS1ToGPRAR.getOut(0)));
        gui.addLabel(new GuiLabel(223, 170, GPRAR.getOut(0)));
        gui.addLabel(new GuiLabel(336, 294, GPRegs.getOut(0)));
        gui.addLabel(new GuiLabel(487, 275, TSBGPRout1.getOut(0)));
        gui.addLabel(new GuiLabel(336, 418, SP.getOut(0)));
        gui.addLabel(new GuiLabel(460, 385, TSBSPout2.getOut(0)));
        gui.addLabel(new GuiLabel(460, 435, TSBADDout2.getOut(0)));
        gui.addLabel(new GuiLabel(336, 445, ADDadr.getOut(0)));
        gui.addLabel(new GuiLabel(500, 441, TSBADDout2.getOut(0)));
        gui.addLabel(new GuiLabel(500, 395, TSBSPout2.getOut(0)));
        gui.addLabel(new GuiLabel(500, 576, TSBCWout3.getOut(0)));
        gui.addLabel(new GuiLabel(336, 574, CW.getOut(0)));
// dovrsi labele :)
    }

    public static Pin SP() {
        return SP.getOut(0);
    }

    public static Pin CW() {
        return CW.getOut(0);
    }

    public static Pin GPRAR() {
        return GPRAR.getOut(0);
    }

    public static int readGPR(int adress) {
        return GPRegs.read(adress);
    }

    public static GPR GPR() {
        return GPRegs;
    }

    public static REG RegGPRAR() {
        return GPRAR;
    }

    public static REG RegSP() {
        return SP;
    }

    public static REG RegCW() {
        return CW;
    }
}
